<template>
  <div>
    <el-tree
      ref="tree"
      :data="list"
      check-strictly
      show-checkbox
      :props="{label: 'name'}"
      node-key="id"
      default-expand-all
    />
    <div style="text-algin:center;">
      <el-button size="medium">取消</el-button>
      <el-button size="medium" type="primary" @click="hSubmit">确定</el-button>
    </div>
  </div>
</template>

<script>
import { getPermissionListAPI } from '@/api/permissions'
import { getTree } from '@/utils/getTree'
import { getRoleDetailAPI, assignPermAPI } from '@/api/setting'
export default {
  props: {
    curId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    // 提交按钮
    hSubmit() {
      this.assignPerm()
    },
    // 初始化数据
    async getPermissionList() {
      try {
        const res = await getPermissionListAPI({ page: 1, pagesize: 100 })
        console.log('', res)
        // 保存数据
        this.list = getTree(res.data)
      } catch (err) {
        console.log('', err)
      }
    },
    // id获取员工基本信息
    async getRoleDetail() {
      try {
        const res = await getRoleDetailAPI(this.curId)
        console.log('', res.data)
        // 保存数据
        // setCheckedKeys():element-ui树形组件自带API 设置复选框选中状态 参数数组
        this.$refs.tree.setCheckedKeys(res.data.permIds)
      } catch (err) {
        console.log('', err)
      }
    },
    // 保存权限分配
    async assignPerm() {
      const permIds = this.$refs.tree.getCheckedKeys()
      try {
        await assignPermAPI({ id: this.curId, permIds: permIds })
        // 分配权限成功
        this.$emit('success')
      } catch (err) {
        console.log('', err)
      }
    }
  }
}
</script>

<style>
</style>
